package controlleur.form;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import annuaire.beans.Admin;
import annuaire.beans.Student;
import annuaire.rmi.remotedObject.IRemotedAnnuaire;
import annuaire.rmi.remotedObject.IRemotedUser;
import form.bean.ChangePwdForm;

/**
 * Contrôleur responsable du fomulaire de changement de mot de passe
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class ChangePwdFormControlleur extends SimpleFormController {

	/** Logger pour cette classe ainsi que pour ses sous classes */
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Reference vers l'objet distant Annuaire
	 */
	private IRemotedAnnuaire remotedAnnuaire;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object commandAjout,
			BindException errors) throws Exception {

		String role = (String) request.getSession().getAttribute("role");
		ChangePwdForm f = (ChangePwdForm) commandAjout;
		IRemotedUser user = (IRemotedUser) request.getSession().getAttribute(
				"user");

		if (role.equals(IRemotedAnnuaire.roleStudent)) {

			Student s = (Student) user.getBeans();

			// on check si l'ancien mot de passe est le bon
			if (!s.getPwd().equals(f.getOldPwd())) {
				errors.rejectValue("oldPwd", "key",
						"Votre ancien mot de passe ne correspond pas");
				return showForm(request, response, errors);
			}

			// on fait l'update
			Student newStudent = s;
			s.setPwd(f.getNewPwd());
			user.updateBeans(newStudent);

			// admin
		} else {
			Admin a = (Admin) user.getBeans();

			// on check si l'ancien mot de passe est le bon
			if (!a.getPwd().equals(f.getOldPwd())) {
				errors.rejectValue("oldPwd", "key",
						"Votre ancien mot de pase ne correspond pas");
				return showForm(request, response, errors);
			}

			// on fait l'update
			Admin newAdmin = a;
			a.setPwd(f.getNewPwd());
			user.updateBeans(newAdmin);
		}

		// MAJ de la session
		request.getSession().setAttribute("user", user);

		ModelAndView mav = showForm(request, response, errors);
		mav.addObject("succesPwd", "Modification effectuée avec succès");
		return mav;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {
		return new ChangePwdForm();
	}

	/**
	 * @param remotedAnnuaire
	 */
	public void setRemotedAnnuaire(IRemotedAnnuaire remotedAnnuaire) {
		this.remotedAnnuaire = remotedAnnuaire;
	}

	/**
	 * @return remotedAnnuaire
	 */
	public IRemotedAnnuaire getRemotedAnnuaire() {
		return remotedAnnuaire;
	}

}
